const randomInt = require('random-int')
const logger = require('./../module/logger')('Importer: Main')
const config = require('./../module/config')
const stack = require('./../module/stack')
const GDImporter = require('./driver/googleDrive')

const startProcess = async (importerClass, doFull) => {
  try {
    await importerClass.run(doFull)
  } catch (error) {
    logger.error(error)
  }
}

const cron = config.get('importer.cron')
logger.debug('Config:', cron)
;(async () => {
  for (const i in cron) {
    const item = cron[i]

    const instance = await stack.getInstance(item.driveId)
    const importerClass = new GDImporter(item.driveId, instance)

    const setCron = async () => {
      logger.debug(`[${item.driveId}] Cron set, ${item.interval}ms`)
      setTimeout(async () => {
        logger.info(`[${item.driveId}] Starting import process`)

        try {
          await startProcess(importerClass, false)
        } catch (e) {
          logger.error(`[${item.driveId}] Import process threw an error`, e)
          setCron()
          return
        }

        logger.info(`[${item.driveId}] Import process fininshed`)
        setCron()
      }, item.interval)
    }

    const queueTime = randomInt(10, 60)

    logger.info(`[${item.driveId}] Ready in ${queueTime} seconds`)

    setTimeout(async () => {
      const doFull = !!(item.doFull)
      logger.info(`[${item.driveId}] Starting first time import process`)

      try {
        await startProcess(importerClass, doFull)
      } catch (e) {
        logger.error(`[${item.driveId}] First time import process threw an error`, e)
        setCron()
        return
      }

      logger.info(`[${item.driveId}] First time import process fininshed`)
      setCron()
    }, queueTime * 1000)
  }
})()
